package cn.itcast.mq.listener;

import cn.itcast.mq.config.CommonConfig;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = CommonConfig.DIRECT_QUEUE)
    public void listenMessage(String msg) {
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
        int i = 1 / 0;
        System.out.println("消息处理成功");
    }

    /**
     * 自定义消费消息失败的处理策略
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "error.queue"),
            exchange = @Exchange(name = "error.exchange", type = ExchangeTypes.DIRECT),
            key = "error.key"))
    public void errorMessage(String msg) {
        System.out.println("消费者接收到失败处理策略的消息：" + msg);
    }

    /**
     * 死信队列的消息监听者
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "dl-queue"),
            exchange = @Exchange(name = "dl-exchange", type = ExchangeTypes.DIRECT),
            key = "dl-key"))
    public void dlMessage(String msg) {
        System.out.println("消费者接收到死信队列的消息：" + msg);
    }

    /**
     * 基于插件的延迟消息监听队列
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "delay-queue"),
            exchange = @Exchange(name = "delay-exchange",delayed = "true"),
            key = "delay-key"))
    public void delayMessage(String msg) {
        System.out.println("基于插件的延迟消息监听队列监听到消息：" + msg);
    }
}
